﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WCFTest.RioWCFServiceReference;
using System.Threading;
using System.IO;

namespace WCFTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        private ManualResetEvent[] resetEvents;
        private List<ReturnDoubleValue> report;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Result.Text = string.Empty;
            resetEvents = new ManualResetEvent[Convert.ToInt16(NbThreads.Text)];
            report = new List<ReturnDoubleValue>();
            for (int s = 0; s < Convert.ToInt16(NbThreads.Text); s++)
            {
                resetEvents[s] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), (object)s);
            }

            //For Each handle As WaitHandle In doneEvents    handle.WaitOne()Next 
            foreach (ManualResetEvent i in resetEvents)
            {
                i.WaitOne();
            }
            
            foreach (ReturnDoubleValue r in report)
            {
                if (!r.IsValid)
                {
                    if (!String.IsNullOrEmpty(Result.Text)) Result.Text += "\n";
                    Result.Text += "Erreur: " + r.ErrorMessage + "\n";
                }
                else
                {
                    if (!String.IsNullOrEmpty(Result.Text)) Result.Text += "\n";
                    Result.Text += "OK\n";
                }
            }

        }


        private void DoWork(object o)
        {
            int index = (int)o;
            WCFServiceClient L_oWCFServiceClient = new WCFServiceClient();  //ServiceManager.GetRioWCFServiceClient();
            try
            {
                //long userId = 110 + index;
                //ReturnDoubleValue db = L_oWCFServiceClient.GetDocumentByFullPath(@"c:\Netbay\Test\test.pdf", 1, 1, ServiceManager.GetLicenseKey(), false, userId);
                //report.Add(db);
                //resetEvents[index].Set();
            }
            catch (Exception e)
            {
                //ReturnDoubleValue d = new ReturnDoubleValue();
                //d.IsValid = false;
                //d.ErrorMessage = e.Message;
                //report.Add(d);
                //resetEvents[index].Set();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string path = @"C:\NetBay\RioScann\cache";
            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(path))
                {
                    // Create the directory if it does not exist.
                    Directory.Delete(path,true);
                }

            }
            catch (Exception)
            {
                Console.WriteLine("The Delete operation failed as expected.");
            }
            finally { }

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            WCFServiceClient L_oWCFServiceClient = new WCFServiceClient();  //ServiceManager.GetRioWCFServiceClient();
            try
            {
                Guid l_oGuid = Guid.NewGuid();
                Dictionary<string, string> FolderIndex = new Dictionary<string,string>();
                Dictionary<string, string> FilePathItem = new Dictionary<string,string>();

                FolderIndex.Add("index1","valeur1");
                FolderIndex.Add("index2", "valeur2");
                FolderIndex.Add("index3", "valeur3");
                FolderIndex.Add("index4", "valeur4");
                FolderIndex.Add("index5","valeur5");


                FilePathItem.Add(@"c:\temp\source\ICSharpCode.SharpZipLib.dll#ICSharpCode.SharpZipLib.dll", "1/2/3/4");


                ReturnBoolean huhu = L_oWCFServiceClient.ExtractFolder(l_oGuid, 100,FolderIndex, FilePathItem, ServiceManager.GetLicenseKey());

                ReturnDoubleValue huhudd = L_oWCFServiceClient.GetExtractedFolders(l_oGuid, ServiceManager.GetLicenseKey());


                //L_oWCFServiceClient.Close();
                using (System.IO.Stream s = System.IO.File.Create(@"c:\uu.zip"))
                {
                    s.Write(huhudd.Value, 0, huhudd.Value.Length);
                }

               L_oWCFServiceClient.DeleteExtractFolder(l_oGuid, ServiceManager.GetLicenseKey());


            }
            catch ( Exception ex)
            {
                string huhu = ex.Message;
            }
        }

      
    }
}
